<?php
/**
 * User: krisbuist
 * Date: 1/19/12
 * Time: 2:56 PM
 */
class Controller extends CI_Controller
{
    /**
     * @var array()
     */
    protected $data;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    public $em;

    /**
     * @var Debugger
     */
    public $debugger;

    /**
     * @var MY_Parser
     */
    public $parser;

    /**
     * @var CI_Session
     */
    public $session;

    /**
     * @var CI_Input
     */
    public $input;

    /**
     * @var CI_Router
     */
    public $router;

    public function __construct()
    {
        parent::__construct();

        $this->em = $this->doctrine->em;
        $this->data = array();

        $user = $this->em->find('User', $this->session->userdata('userId'));

        if ($user != null)
        {
            $myChannels = $user->getChannelSubscriptions();
            $myCategories = $user->getCategorySubscriptions();

            $this->data['myChannels'] = $myChannels;
            $this->data['myCategories'] = $myCategories;
        }
        $this->data['headerCategories'] = $this->em->getRepository('Category')->findAll();
        $channels = $this->em->createQuery("SELECT c FROM Channel c ORDER BY c.id DESC")->setMaxResults(8);
        $this->data['headerChannels'] = $channels->getResult();
        $this->data['title'] = "Nieuwsbuis, waar jij het nieuws bepaald.";
        $this->data['styles'] = array();
        $this->data['styles'][] = "/public/css/header.css";



        if ($this->session->userdata('loggedin')) {
            $this->data['authenticated_user'] = $this->em->find('User', $this->session->userdata('userId'));
        }

        $permissions = $this->config->item('permissions');
        $controller = $this->router->fetch_class();
        $action = $this->router->fetch_method();
        $accessLevel = max($this->session->userdata('accessLevel') , 0);

        //var_dump($accessLevel);
        //die();


        if(!isAuthorized($permissions,$controller,$action,$accessLevel))
        {
            redirect("/auth/login", "location");
        }
    }
}
